package com.mahjong.server.protocolHandler.user;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.mahjong.server.common.Common;
import com.mahjong.server.mina.protocol.AbsMessageProtocol;
import com.mahjong.server.mina.protocol.DataBuf;
import com.mahjong.server.mina.protocol.MessageProtocol;
import com.mahjong.server.model.User;
import com.mahjong.server.service.UserService;
import com.mahjong.server.util.Utils;

/**
 * 
 * @author Simple
 * @date 2013-3-5 上午10:59:26
 * @Description 注册用户请求
 */
@Component
@Scope("prototype")
public class RegisterReq extends MessageProtocol {

	private static final byte TAG = Common.Req;

	private static final short PROTOCOL_NUM = 0x0001;

	private Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private UserService userService;

	private String username;

	private String password;

	@Override
	public short getProtocolNum() {
		return PROTOCOL_NUM;
	}

	@Override
	public byte getTag() {
		return TAG;
	}

	@Override
	public AbsMessageProtocol execute(IoSession session, AbsMessageProtocol req) {
		log.debug(this.getClass().getSimpleName() + " execute");
		if (Utils.isNotNull(username) && Utils.isNotNull(password)) {
			if (null == userService.getByName(username)) {
				User user = new User();
				user.setUsername(username);
				user.setPassword(password);
				userService.save(user);
				log.debug("注册成功");
				return new RegisterResp(Common.SUCCESS);
			} else {
				log.debug("用户名[" + username + "]已存在");
				return new RegisterResp(Common.FAILD);
			}
		} else {
			log.debug("用户名或密码为空");
			return new RegisterResp((byte) 2);
		}

	}

	@Override
	public void reqDecode(DataBuf buf) {
		log.debug(this.getClass().getSimpleName() + " decode");
		username = buf.getString();
		password = buf.getString();
	}
}
